package cn.domypay.mail.thread;

import java.util.List;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import cn.domypay.back.log.DoMyPayLogger;
import cn.domypay.mail.base.SmtpBiz;

public class MainSenderThread extends Thread {
	
	private static DoMyPayLogger logger = DoMyPayLogger.getLogger(MainSenderThread.class);
	
	private Transport transport;
	
	private Message message;
	
	private List<String> resultList;
	
	public MainSenderThread(Transport transport,Message message,List<String> resultList){
		
		this.transport = transport;
		this.message = message;
	}
	public MainSenderThread(ThreadGroup group,String name,Transport transport,Message message,List<String> resultList){
		super(group, name);
		this.transport = transport;
		this.message = message;
		this.resultList = resultList;
	}
	
	
	/**
	 * 建立一个线程，处理邮件发送
	 */
	public void run()
	{
		SmtpBiz smtpBiz = new SmtpBiz();
		/*
		 * 执行发送邮件
		 */
		try {
			smtpBiz.smtpSenderInThread(transport, message);
		} catch (MessagingException e) {
			// TODO �Զ���� catch ��
			/**
			 * 如果邮件发送不成功，则把收件用户的地址放入结果集
			 */
			InternetAddress[] address;
			try {
				address = (InternetAddress[])message.getAllRecipients();
				for(int i=0;i<address.length;i++)
				{
					resultList.add(address[i].getAddress());
				}
			} catch (MessagingException e1) {
				// TODO �Զ���� catch ��
				logger.logInfo(e);
			}
			logger.logInfo(e);
		}
	}
	
	
	public void senderStop(Transport transport) throws MessagingException
	{
		if(transport.isConnected())
		{
			transport.close();
			this.interrupt();
		}
	}
}
